package com.atguigu.tingshu.account.mq;


import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Slf4j
@Component
public class AccountReceiver {

    @Autowired
    private UserAccountService userAccountService;

    @KafkaListener(topics = KafkaConstant.QUEUE_USER_REGISTER)
    public void addUserAccount(ConsumerRecord<String, String> record) {
        String value = record.value();

        if (value != null) {
            userAccountService.addUserAccount(value);
        }

    }


    @KafkaListener(topics = KafkaConstant.QUEUE_ACCOUNT_MINUS)
    public void minusAccount(ConsumerRecord<String , String> record) {
        String value = record.value();
        if(!StringUtils.isEmpty(value)) {
            userAccountService.minusAccount(value) ;
            log.info("账户的余额扣减成功了...orderNo: {}" , value);
        }
    }
    @KafkaListener(topics = KafkaConstant.QUEUE_ACCOUNT_UNLOCK)
    public void unLockAccount(ConsumerRecord<String , String> record) {
        String value = record.value();
        if(!StringUtils.isEmpty(value)) {
            userAccountService.unLockAccount(value) ;
            log.info("账户锁定金额解锁成功...orderNo: {}" , value);
        }
    }
}
